using MediatR;
using SumerCoreDevOps.Application.UserOperationLogs.Queries;
using SumerCoreDevOps.Domain.Entities;
using SumerCoreDevOps.Infrastructure.Identity;

namespace SumerCoreDevOps.Application.UserOperationLogs.Handlers;

public class GetLogsByUserIdQueryHandler : IRequestHandler<GetLogsByUserIdQuery, List<UserOperationLog>>
{
    private readonly UserOperationLogManager _logManager;

    public GetLogsByUserIdQueryHandler(UserOperationLogManager logManager)
    {
        _logManager = logManager;
    }

    public async Task<List<UserOperationLog>> Handle(GetLogsByUserIdQuery request, CancellationToken cancellationToken)
    {
        return await _logManager.GetLogsByUserIdAsync(request.UserId, request.Limit, request.Offset);
    }
}

public class GetLogsByOperationTypeQueryHandler : IRequestHandler<GetLogsByOperationTypeQuery, List<UserOperationLog>>
{
    private readonly UserOperationLogManager _logManager;

    public GetLogsByOperationTypeQueryHandler(UserOperationLogManager logManager)
    {
        _logManager = logManager;
    }

    public async Task<List<UserOperationLog>> Handle(GetLogsByOperationTypeQuery request, CancellationToken cancellationToken)
    {
        return await _logManager.GetLogsByOperationTypeAsync(request.OperationType, request.Limit, request.Offset);
    }
}

public class GetRecentLogsQueryHandler : IRequestHandler<GetRecentLogsQuery, List<UserOperationLog>>
{
    private readonly UserOperationLogManager _logManager;

    public GetRecentLogsQueryHandler(UserOperationLogManager logManager)
    {
        _logManager = logManager;
    }

    public async Task<List<UserOperationLog>> Handle(GetRecentLogsQuery request, CancellationToken cancellationToken)
    {
        return await _logManager.GetRecentLogsAsync(
            request.Limit,
            request.Username,
            request.OperationType,
            request.StartTime,
            request.EndTime
        );
    }
}

public class GetFailedLogsQueryHandler : IRequestHandler<GetFailedLogsQuery, List<UserOperationLog>>
{
    private readonly UserOperationLogManager _logManager;

    public GetFailedLogsQueryHandler(UserOperationLogManager logManager)
    {
        _logManager = logManager;
    }

    public async Task<List<UserOperationLog>> Handle(GetFailedLogsQuery request, CancellationToken cancellationToken)
    {
        return await _logManager.GetFailedLogsAsync(request.Limit, request.StartTime);
    }
}

public class GetOperationStatisticsQueryHandler : IRequestHandler<GetOperationStatisticsQuery, Dictionary<string, long>>
{
    private readonly UserOperationLogManager _logManager;

    public GetOperationStatisticsQueryHandler(UserOperationLogManager logManager)
    {
        _logManager = logManager;
    }

    public async Task<Dictionary<string, long>> Handle(GetOperationStatisticsQuery request, CancellationToken cancellationToken)
    {
        return await _logManager.GetOperationStatisticsAsync(request.StartTime, request.EndTime);
    }
}
